package com.iasku.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ksoap2.serialization.SoapObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.iasku.constant.Constant;
import com.iasku.constant.IntentConfig;
import com.iasku.entity.IaskuCourse;
import com.iasku.iaskueleclass_phone.R;
import com.iasku.iaskueleclass_phone.activity.VideoPlayActivity;
import com.iasku.util.SoapUtil;
import com.iasku.web.XmlParser;

public class CourseAdapter extends BaseAdapter {
    private static final String STATUS_YUXI = "0";
    private static final String STATUS_FUXI = "1";
    
	private Context context;
	private LayoutInflater inflater;
	private List<IaskuCourse> data;
	private ProgressDialog pd;

	public CourseAdapter(Context context, List<IaskuCourse> data) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.data = data;
		pd = new ProgressDialog(context);
		pd.setMessage("正在加载...");
	}

	@Override
	public int getCount() {

		return data.size();
	}

	@Override
	public Object getItem(int position) {

		return data.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.course_item, null);
			holder = new ViewHolder();
			holder.coursename = (TextView) convertView
					.findViewById(R.id.tv_course_name);
			holder.teacher = (TextView) convertView
					.findViewById(R.id.tv_course_teachername);
			holder.begintime = (TextView) convertView
					.findViewById(R.id.tv_course_starttime);
			holder.yuxi = (TextView) convertView.findViewById(R.id.btn_course_yuxi);
			holder.fuxi = (TextView) convertView.findViewById(R.id.btn_course_fuxi);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final IaskuCourse sc = data.get(position);

		holder.coursename.setText(sc.getCoursename());
		holder.teacher.setText(sc.getTeacherid());
		holder.begintime.setText(sc.getBegintime());
		final String courseid = sc.getCourseid();
		final String state = sc.getClassstate();
		if (sc.isEleClass()) {
			holder.yuxi.setVisibility(View.INVISIBLE);
			holder.fuxi.setVisibility(View.INVISIBLE);
			if (state.startsWith("1")) {
				holder.yuxi.setVisibility(View.VISIBLE);
			}
			if (state.endsWith("1")) {
				holder.fuxi.setVisibility(View.VISIBLE);
			}
		}

		holder.yuxi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				pd.show();
				getCourseVideo(courseid, STATUS_YUXI);
			}
		});

		holder.fuxi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				pd.show();
				getCourseVideo(courseid, STATUS_YUXI);
			}
		});

		return convertView;
	}


	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case -1:
				Toast.makeText(context, msg.obj.toString(), Toast.LENGTH_SHORT)
						.show();
				break;
			case 1:
				getVideoByCourseid(msg);
				break;
			}
		}
	};

	private void getVideoByCourseid(Message msg) {
		pd.dismiss();
		Map<String, String> map = XmlParser.getVideoByCourseid(msg.obj
				.toString());
		if (map == null || map.isEmpty()) {
			return;
		}
		Intent intent = new Intent();
		intent.setClass(context, VideoPlayActivity.class);
		intent.putExtra(IntentConfig.QUE_VIDEO_PATH, map.get("filename"));
		context.startActivity(intent);
	}

	private void getCourseVideo(final String courseid, final String state) {
		new Thread() {
			public void run() {
				String methodname = "GetCourseVideo";
				String resultLog = methodname + Constant.RESULT;
				HashMap<String, String> params = new HashMap<String, String>();
				params.put("courseid", courseid);
				params.put("state", state);
				SoapObject res = SoapUtil.getResponseSoapObject(params, methodname, Constant.NAMESPACE2, Constant.URL2);
				Message msg = Message.obtain();
				msg.what = 1;
				msg.obj = res.getProperty(resultLog);
				mHandler.sendMessage(msg);
			}
		}.start();
	}

	private class ViewHolder {
		TextView coursename;
		TextView teacher;
		TextView begintime;
		TextView yuxi,fuxi;
	}
}
